package test.vo
{
	import _Pan3D.display3D.Display3DSprite;
	import _Pan3D.display3D.analysis.AnalysisServer;
	import _Pan3D.load.LoadInfo;
	import _Pan3D.load.LoadManager;
	import _Pan3D.program.Program3DManager;
	import _Pan3D.program.shaders.GroundScanningHightShader;
	import _Pan3D.texture.TextureManager;
	
	import _me.Scene_data;
	
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.Program3D;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	
	public class GroundDisplay3DSprite extends Display3DSprite
	{
		public function GroundDisplay3DSprite(context:Context3D)
		{
			super(context);
		}
		private function get sunMatrix3D():Matrix3D
		{
			var sunMatrix3D:Matrix3D=new Matrix3D;
			sunMatrix3D.prepend(Scene_data.sun3D.cameraMatrix);
			sunMatrix3D.prepend(posMatrix);
			return sunMatrix3D
		}
		private function get lightMatrix3D():Matrix3D
		{
			var lightMatrix3D:Matrix3D=new Matrix3D;
			lightMatrix3D.prepend(Scene_data.light3D.cameraMatrix);
			lightMatrix3D.prepend(posMatrix);
			return lightMatrix3D
		}
		override public function updataAndScanning():void
		{
			if (!this._visible) {
				return;
			}
			if (_objData && _objData.texture) {
				//选择用扫描地形的着色器
				var tempPrograme3D:Program3D = Program3DManager.getInstance().getProgram(  GroundScanningHightShader.GROUND_SCANNING_HIGHT_SHADER);
				_context.setProgram(tempPrograme3D);
				setScanningVc();
				setScanningVa();
				resetScanningVa();
			}
		}
		override protected function setScanningVa() : void {
			_context.setVertexBufferAt(0, _objData.vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setVertexBufferAt(1, _objData.uvBuffer, 0, Context3DVertexBufferFormat.FLOAT_2);
			_context.setTextureAt(0, _objData.texture);
			_context.drawTriangles(_objData.indexBuffer, 0, -1);
		}
		
		override protected function resetScanningVa() : void {
			_context.setVertexBufferAt(0, null);
			_context.setVertexBufferAt(1, null);
			_context.setTextureAt(0,null);
		}
		override protected function setScanningVc() : void {
			this.updateMatrix();
			//modelMatrix.prependScale(2,0.2,2)
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, modelMatrix, true);
			//传入一个缩放比值进来让显示对应像在平行线上1024也就是用来存取的大小，暂时统一;
			var _grounHightMapSize:int=Scene_data.grounHightMapSize;
			var _grounHightMapScale:int=Scene_data.grounHightMapScale;
			_context.setProgramConstantsFromVector(Context3DProgramType.VERTEX, 8, Vector.<Number>([_grounHightMapSize*_grounHightMapScale, 0, 0, 0]));
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,5, Vector.<Number>([256, 256, 1, 1]));
		}
		override protected function setVc() : void {
			this.updateMatrix();
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, modelMatrix, true);
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 8, sunMatrix3D, true);
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 12, lightMatrix3D, true);
			
		
		

		//	var angle:Vector3D = new Vector3D(0.5,0.5,0.5,0);
			var angle:Vector3D = new Vector3D(Scene_data.sun3D.x,Scene_data.sun3D.y,Scene_data.sun3D.z,0);
			angle.normalize();
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,0,Vector.<Number>([angle.x,angle.y,angle.z,angle.w]));
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,1,Vector.<Number>( [Scene_data.radd,Scene_data.gadd,Scene_data.badd,0.5 ]));
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,2, Vector.<Number>([0, 0, 0.5, 1]));

			
		}
		override protected function setVa() : void {
			_context.setVertexBufferAt(0, _objData.vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setVertexBufferAt(1, _objData.uvBuffer, 0, Context3DVertexBufferFormat.FLOAT_2);
			_context.setVertexBufferAt(2,_objData.normalsBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setTextureAt(0, Scene_data.sceneLightText);
			_context.setTextureAt(1, _objData.texture);
			_context.setTextureAt(2,  Scene_data.texShadowMap);   //传入阴影体贴。用于对比是否背遮挡
			_context.drawTriangles(_objData.indexBuffer, 0, -1);
		}
		override protected function resetVa() : void {
			_context.setVertexBufferAt(0, null);
			_context.setVertexBufferAt(1, null);
			_context.setVertexBufferAt(2, null);
			_context.setTextureAt(0,null);
			_context.setTextureAt(1,null);
			_context.setTextureAt(2,null);
		}
		override public function set url(value : String) : void {
			_url = value;
			var loaderinfo : LoadInfo = new LoadInfo(Scene_data.fileRoot+"ground/obj/"+ _url + ".xml", LoadInfo.XML, onObjLoad, false);
			LoadManager.getInstance().addSingleLoad(loaderinfo);
		}
		
		override protected function onObjLoad(str : String) : void {
			_objData = AnalysisServer.getInstance().analysisObj(str);
			TextureManager.getInstance().addTexture(Scene_data.fileRoot+"ground/texture/" + _objData.mtl + ".jpg", addTexture, null);
		}

	}
}